package dobest.ray.com.jiongjiong.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;

/**
 * 自定义接收器
 * 
 * 如果不定义这个Receiver，则接收不到自定义消
 */
public class JPushReceiver extends BroadcastReceiver {
	private static final String TAG = "JPush";

	@Override
	public void onReceive(Context context, Intent intent) {
		Bundle bundle = intent.getExtras();
		Log.d("wanglei", "[MyReceiver] onReceive - " + intent.getAction()
				+ ", extras: " + printBundle(bundle));

		if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
			String regId = bundle
					.getString(JPushInterface.EXTRA_REGISTRATION_ID);
			// Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
			// send the Registration Id to your server...

		} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent
				.getAction())) {
			// 接收自定义消息
			// Log.d("wanglei",
			// "[MyReceiver] 接收到推送下来的自定义消息： "
			// + bundle.getString(JPushInterface.EXTRA_MESSAGE));
			// Toast.makeText(
			// context,
			// "接收到推送消息：" + bundle.getString(JPushInterface.EXTRA_MESSAGE),
			// 1).show();
			processCustomMessage(context, bundle);

		} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent
				.getAction())) {

			// 发送广播告诉主页面取消icon
//			Intent it = new Intent(Constant.MAIN_RECEIVE_ACTION);
//			it.putExtra("Icon", true);
//			context.sendBroadcast(it);

			// 更新数据
//			Intent it1 = new Intent(Constant.MAIN_RECEIVE_REFRESH);
//			context.sendBroadcast(it1);

			// 接收通知
			// Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");
			int notifactionId = bundle
					.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
			// Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);

		} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent
				.getAction())) {
			// 点击通知 跳转activity
			// Log.d(TAG, "[MyReceiver] 用户点击打开了通知");

			// 打开自定义的Activity
			String json = bundle.getString(JPushInterface.EXTRA_EXTRA);

			String type = "";
			String parttimeId = null;
			try {
				JSONObject jo = new JSONObject(json);
				parttimeId = jo.getString("parttimeId");
				type = jo.getString("type");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			// if (type == null || parttimeId == null) {
			// Log.i("wanglei", "null");
			// return;
			// }
//			Intent i = null;
			// 0：消息列表 1：企业端兼职详情 2： 求职端工作申请详情
			Log.i("wanglei", "type:" + type + "|parttimeId:" + parttimeId);
			if ("1".equals(type) && parttimeId != null) {
//				i = new Intent(context, PartTimeJobDetailActivity.class);
//				i.putExtra("parttimeTypeId", Long.parseLong(parttimeId));
			} else if ("2".equals(type) && parttimeId != null) {
				Log.i("wanglei", "进入收到的申请");
//				i = new Intent(context, JobApplicationDetailActivity.class);
//				i.putExtra("parttimeId", parttimeId);
			} else {

//				i = new Intent(context, MainActivity.class);
//				i.putExtras(bundle);
//				i.putExtra("startType", 2);
				// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
				// | Intent.FLAG_ACTIVITY_CLEAR_TOP);

			}
//			if (i != null) {
//				i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
//						| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
//						| Intent.FLAG_ACTIVITY_CLEAR_TOP);
//				context.startActivity(i);
//			}
		} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent
				.getAction())) {
			// Log.d(TAG,
			// "[MyReceiver] 用户收到到RICH PUSH CALLBACK: "
			// + bundle.getString(JPushInterface.EXTRA_EXTRA));
			// 在这里根�?JPushInterface.EXTRA_EXTRA
			// 的内容处理代码，比如打开新的Activity�?打开�?��网页�?.

		} else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent
				.getAction())) {
			boolean connected = intent.getBooleanExtra(
					JPushInterface.EXTRA_CONNECTION_CHANGE, false);
			// Log.w(TAG, "[MyReceiver]" + intent.getAction()
			// + " connected state change to " + connected);
		} else {
			// Log.d(TAG, "[MyReceiver] Unhandled intent - " +
			// intent.getAction());
		}
	}

	// 拼接intent extra 数据
	private static String printBundle(Bundle bundle) {

		StringBuilder sb = new StringBuilder();
		for (String key : bundle.keySet()) {
			if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
				sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
			} else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) {
				sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
			} else {
				sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
			}
		}
		return sb.toString();
	}

	// send msg to MainActivity
	private void processCustomMessage(Context context, Bundle bundle) {

//		if (NewsFragment.isForeground) {
//			String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
//			String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
//			Intent msgIntent = new Intent(NewsFragment.MESSAGE_RECEIVED_ACTION);
//			msgIntent.putExtra(NewsFragment.KEY_MESSAGE, message);
//			if (!JPushUtils.isEmpty(extras)) {
//				try {
//					JSONObject extraJson = new JSONObject(extras);
//					if (null != extraJson && extraJson.length() > 0) {
//						msgIntent.putExtra(NewsFragment.KEY_EXTRAS, extras);
//					}
//				} catch (JSONException e) {
//
//				}
//
//			}
//			context.sendBroadcast(msgIntent);
//		}
	}
}
